#include "ThreadPool.hpp"
#include "Task.hpp"
const string ops = "+-*/%";
void *consumer(void *args)
{
    BlockQueue<Task> *bqp = static_cast<BlockQueue<Task> *>(args);
    while (true)
    {
        Task t = bqp->pop();
        int result = t();
        int one, two;
        char op;
        t.get(&one, &two, &op);
        cout << "consumer[" << pthread_self() << "]" << (unsigned int)time(nullptr)
             << "消费了一个任务：" << one << op << two << "=" << result << endl;
    }
}
void *producer(void *args)
{
    BlockQueue<Task> *bqp = static_cast<BlockQueue<Task> *>(args);
    while (true)
    {
        int one = rand() % 50;
        int two = rand() % 20;
        char op = ops[rand() % ops.size()];
        Task t(one, two, op);
        bqp->push(t);
        cout << "producer[" << pthread_self() << "]" << (unsigned int)time(nullptr)
             << "生产了一个任务：" << one << op << two << "=?" << endl;
        sleep(1);
    }
}
int main()
{
    srand((unsigned int)time(nullptr) ^ getpid());
    BlockQueue<Task> bq;
    pthread_t c, p;
    pthread_create(&p, nullptr, producer, (void *)&bq);
    pthread_create(&c, nullptr, consumer, (void *)&bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    return 0;
}